﻿<common:LayoutAwarePage 
    x:Name="pageRoot"
    x:Class="SuperQuiz.QuizPlayPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    IsTabStop="false"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SuperQuiz"
    xmlns:logic="using:SuperQuiz.Logic"
    xmlns:utils="using:SuperQuiz.Utils"
    xmlns:view="using:SuperQuiz.View" 
    xmlns:common="using:SuperQuiz.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <utils:StringToImageConverter x:Key="StringToImageConverter"/>
        <common:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <utils:StringIsNullOrEmptyToVisibilityConverter x:Key="StringIsNullOrEmptyToVisibilityConverter"/>
    </Page.Resources>
    <common:LayoutAwarePage.BottomAppBar>
        <AppBar IsSticky="True" IsOpen="True" Closed="AppBarClosed">
            <Grid x:Name="gridAppBar">
                <StackPanel Orientation="Horizontal">
                    <Button x:Uid="AppBarPrevious" Style="{StaticResource PreviousAppBarButtonStyle}" Click="PreviousClick" IsEnabled="{Binding QuizPlay.IsPreviousEnabled}" Visibility="{Binding QuizPlay.IsPreviousVisible, Converter={StaticResource BooleanToVisibilityConverter}}"/>
                </StackPanel>
                <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
                    <Button x:Name="askHelpButton" x:Uid="AppBarAskHelp"  Style="{StaticResource AppBarButtonStyle}" Content="&#xE200;" Click="ShareClick" Visibility="{Binding QuizPlay.IsAskHelpVisible, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    <Button x:Name="nextButton" x:Uid="AppBarNext" Style="{StaticResource NextAppBarButtonStyle}" Click="NextClick" IsEnabled="{Binding QuizPlay.IsNextEnabled}" Visibility="{Binding QuizPlay.IsNextVisible, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    <Button x:Name="finishButton" x:Uid="AppBarFinish" Style="{StaticResource YesAppBarButtonStyle}" Click="FinishClick" Visibility="{Binding QuizPlay.IsFinishVisible, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    <Button x:Name="resultsButton" x:Uid="AppBarResults" Style="{StaticResource YesAppBarButtonStyle}" Click="ResultsClick" Visibility="{Binding QuizPlay.IsResultsVisible, Converter={StaticResource BooleanToVisibilityConverter}}" />
                </StackPanel>
            </Grid>
        </AppBar>
    </common:LayoutAwarePage.BottomAppBar>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid
        Style="{StaticResource LayoutRootStyle}"
        DataContext="{Binding QuizPlay}"
        d:DataContext="{Binding Categories[0].Quizzes[0], Source={d:DesignInstance Type=logic:QuizApplication, IsDesignTimeCreatable=True}}">

        <Grid.RowDefinitions>
            <RowDefinition Height="140" x:Name="headerRow"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Text="{Binding Quiz.Name}" Style="{StaticResource PageHeaderTextStyle}" Grid.Column="1"/>
            <Grid Grid.Column="2" x:Name="scorePanel" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,30,40" Visibility="{Binding Quiz.ShowScoreWhilePlays, Converter={StaticResource BooleanToVisibilityConverter}}">
                <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom">
                    <TextBlock x:Name="scoreText" Text="{Binding FinalScore}" Style="{StaticResource HeaderTextStyle}"/>
                    <Path x:Name="scoreStar" Data="M232.83511,311.94046 L328.35764,141.791 L422.38788,313.43289 L608.95538,347.76135 L474.62683,488.06033 L501.49258,679.10577 L328.35773,595.52338 L153.7303,680.59827 L177.61131,488.06061 L46.267715,347.76151 z" Stretch="UniformToFill" UseLayoutRounding="False" StrokeThickness="2" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeDashCap="Round" StrokeLineJoin="Round" StrokeMiterLimit="0" Style="{StaticResource WinningPointsStarStyle}" Width="35" Height="35" VerticalAlignment="Bottom" />
                </StackPanel>
                <Canvas x:Name="starsPlaceHolder" />
            </Grid>
        </Grid>

        <Grid
            Grid.Row="1"
            Margin="100,0,0,88" x:Name="questionDetail">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <StackPanel Margin="17,0,17,8">
                <TextBlock Text="{Binding CurrentQuestion.Title}" TextWrapping="Wrap" TextTrimming="WordEllipsis" Style="{StaticResource SubheaderTextStyle}"/>
                <TextBlock Text="{Binding CurrentQuestion.SubTitle}" Visibility="{Binding  CurrentQuestion.SubTitle, Converter={StaticResource StringIsNullOrEmptyToVisibilityConverter}}" TextWrapping="Wrap" TextTrimming="WordEllipsis" Style="{StaticResource TitleTextStyle}"/>
            </StackPanel>
            <ScrollViewer Margin="17,0,0,0" Grid.Row="1" Style="{StaticResource NoScrollViewerStyle}" x:Name="questionScrollViewer">
                <Grid Margin="0,0,17,0" >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" x:Name="questionRow1" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition x:Name="separatorColumn" Width="80" />
                        <ColumnDefinition x:Name="questionAnswersColumn" />
                    </Grid.ColumnDefinitions>
                    <ScrollViewer Style="{StaticResource VerticalScrollViewerStyle}" x:Name="questionDetailScrollViewer">
                        <StackPanel Margin="0,0,8,0">
                            <TextBlock Text="{Binding CurrentQuestion.Description}" TextWrapping="Wrap" TextTrimming="WordEllipsis" Style="{StaticResource LargeTextBlockTextStyle}"/>
                            <Image Source="{Binding CurrentQuestion.ImageUri, Converter={StaticResource StringToImageConverter}}"  Visibility="{Binding  CurrentQuestion.ImageUri, Converter={StaticResource StringIsNullOrEmptyToVisibilityConverter}}" Stretch="Uniform" Margin="8,8,8,0" HorizontalAlignment="Left" Height="200"/>
                            <view:QuizLinksListControl Margin="0,8,0,0" DataContext="{Binding CurrentQuestion.Links}" Height="Auto" VerticalAlignment="Top" />
                        </StackPanel>
                    </ScrollViewer>                    
                    <Grid Grid.Column="2" x:Name="answersGrid" Margin="2,0,0,0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="Answers" x:Uid="AnswersTextBlock"  Style="{StaticResource TitleTextStyle}" Margin="0,0,0,4"/>
                        <ListView x:Name="answersList" Grid.Row="1" ItemsSource="{Binding CurrentQuestion.Answers}" IsEnabled="{Binding CurrentAnswerSelectionEnabled}" ItemContainerStyle="{StaticResource QuizAnswerListViewItemStyle}" SelectionChanged="AnswerSelected" Margin="4,0,0,0" />
                    </Grid>    
                </Grid>
            </ScrollViewer>
        </Grid>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The back button respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="questionDetail" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0,0,0,88"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="scoreStar" Storyboard.TargetProperty="Width">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="20"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="scoreStar" Storyboard.TargetProperty="Height">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="20"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="scorePanel" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0,0,18,40"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="answersGrid" Storyboard.TargetProperty="(Grid.Row)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="questionAnswersColumn" Storyboard.TargetProperty="Width">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="separatorColumn" Storyboard.TargetProperty="Width">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="questionRow1" Storyboard.TargetProperty="Height">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Auto"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="answersGrid" Storyboard.TargetProperty="(Grid.Column)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="scoreText" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SubheaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="questionScrollViewer" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource VerticalScrollViewerStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="questionDetailScrollViewer" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource NoScrollViewerStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="answersList" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource NoScrollListView}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="headerRow" Storyboard.TargetProperty="Height">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="80"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="gridAppBar" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="-20,0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="askHelpButton" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="-30,0,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="nextButton" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="-30,0,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="finishButton" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="-30,0,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="resultsButton" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="-30,0,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
